class Solution
{
public:
    int longestPalindrome(vector<string> &words)
    {
        unordered_map<string, int> nonRepeatWordComp;
        unordered_set<string> repeatWord;
        int len = 0;
        for (auto &word : words)
        {
            if (word[0] == word[1])
            {
                if (repeatWord.count(word))
                {
                    len += 4;
                    repeatWord.erase(word);
                }
                else
                {
                    repeatWord.insert(word);
                }
            }
            else
            {
                if (nonRepeatWordComp.count(word) && nonRepeatWordComp[word] != 0)
                {
                    len += 4;
                    --nonRepeatWordComp[word];
                }
                else
                {
                    string wordComp(word.rbegin(), word.rend());
                    ++nonRepeatWordComp[wordComp];
                }
            }
        }
        if (repeatWord.size())
        {
            len += 2;
        }
        return len;
    }
};